#include "http_server.h"

static const char *TAG = "http server";
#define URI_title "/wifi"
const char equal = '=';
const char s[2] = "&";

// An HTTP GET handler
// 连接wifi浏览器输入 http://192.168.199.196/wifi?ssid=Meta&password=metatype123&noname=end 具体ip地址看log
// 连接AP浏览器输入 http://192.168.4.1/wifi?ssid=Meta&password=metatype123&noname=end
static esp_err_t root_wifi_handler(httpd_req_t *req)
{
    static char dest[500];

    httpd_resp_set_type(req, "application/json"); //反馈 作为请求头告诉服务端消息主体是序列化的JSON字符串
    httpd_resp_send(req, "true", -1);             // -1 = use strlen()

    ESP_LOGI(TAG, "Current url is: %s", req->uri); //打印包含“/wifi”的请求

    strcpy(dest, req->uri);
    ESP_LOGI(TAG, "Copy url is: %s", dest); //复制信息防止丢失

    //获取wifi ssid
    g_wifi_ssid = strtok(dest, s);            //分解字符串
    g_wifi_ssid = strchr(g_wifi_ssid, equal); //返回第一次出现“=”后的字符串
    g_wifi_ssid++;                            //左移一位

    //获取wifi password
    g_wifi_password = strtok(NULL, s);                //提取剩下的部分
    g_wifi_password = strchr(g_wifi_password, equal); //返回第一次出现“=”后的字符串
    g_wifi_password++;                                //左移一位

    if ((strlen(g_wifi_password) != 0) && (strlen(g_wifi_ssid) != 0))
    {
        ESP_LOGI(TAG, "extract wifi PASSWORD is: %s", g_wifi_password);
        ESP_LOGI(TAG, "extract wifi SSID is: %s", g_wifi_ssid);
        WIFI_Connect_Trigger();
    }
    // ESP_LOGI(TAG, "received wifi imformation form http server %d", wireless_state.station_inform_state);
    //ESP_ERROR_CHECK(wifi_sta_init());//开启apstation模块 并尝试连接wifi

    return ESP_OK;
}

// static const httpd_uri_t root = {
//     .uri = "/wifi", //响应的请求名称
//     .method = HTTP_GET,
//     .handler = root_wifi_handler};

//建立http server
//通过从AP访问 http://192.168.4.1/ 获取反馈
// esp_err_t wifi_softap_server(void)
// {
//     httpd_handle_t server = NULL;

//     // Start the httpd server
//     ESP_LOGI(TAG, "Starting server");

//     // httpd_ssl_config_t conf = HTTPD_SSL_CONFIG_DEFAULT();
//     httpd_config_t conf = HTTPD_DEFAULT_CONFIG(); //配置httpd

//     // extern const unsigned char cacert_pem_start[] asm("_binary_cacert_pem_start");
//     // extern const unsigned char cacert_pem_end[]   asm("_binary_cacert_pem_end");
//     // conf.cacert_pem = cacert_pem_start;
//     // conf.cacert_len = cacert_pem_end - cacert_pem_start;

//     // extern const unsigned char prvtkey_pem_start[] asm("_binary_prvtkey_pem_start");
//     // extern const unsigned char prvtkey_pem_end[]   asm("_binary_prvtkey_pem_end");
//     // conf.prvtkey_pem = prvtkey_pem_start;
//     // conf.prvtkey_len = prvtkey_pem_end - prvtkey_pem_start;

//     esp_err_t ret = httpd_start(&server, &conf); //开启http服务
//     if (ESP_OK != ret)
//     {
//         ESP_LOGI(TAG, "Error starting server!");
//         return ESP_OK;
//     }

//     // Set URI handlers
//     ESP_LOGI(TAG, "Registering URI handlers");
//     httpd_register_uri_handler(server, &root); //请求处理
//     //return server;

//     if (server != NULL)
//     { //确认server是否建立完成
//         return ESP_OK;
//     }
//     return ESP_FAIL;
// }

//关闭http服务器
// esp_err_t stop_webserver(httpd_handle_t server)
// {
//     // Stop the httpd server
//     httpd_ssl_stop(server);
//     return ESP_OK;
// }

/*
static void disconnect_handler(void* arg, esp_event_base_t event_base, 
                               int32_t event_id, void* event_data)
{
    httpd_handle_t* server = (httpd_handle_t*) arg;
    if (*server) {
        stop_webserver(*server);
        *server = NULL;
    }
}

static void connect_handler(void* arg, esp_event_base_t event_base, 
                            int32_t event_id, void* event_data)
{
    httpd_handle_t* server = (httpd_handle_t*) arg;
    if (*server == NULL) {
        *server = wifi_softap_server();
        ESP_LOGI(TAG, "Web server started...");
    }
}
*/